
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:psi/theme/app_colors.dart';
import 'package:psi/widgets/text_widget.dart';
import 'package:shake_animation_widget/shake_animation_widget.dart';

class ShakeTabButton extends StatefulWidget {
  final String imageName;
  final String title;
  final Color textColor;
  final Color seletedTextColor;
  final bool isSelected;
  final ShakeAnimationType shakeAnimationType;
  final VoidCallback? onPressed;

  ShakeTabButton({required this.imageName, required this.title, this.textColor = const Color(0xFF999999), this.seletedTextColor = primaryColor, this.isSelected =
      false, this
      .shakeAnimationType =
      ShakeAnimationType.RoateShake, this.onPressed});

  @override
  _ShakeTabButtonState createState() => _ShakeTabButtonState();
}

class _ShakeTabButtonState extends State<ShakeTabButton> {
  ///抖动动画控制器
  final ShakeAnimationController _shakeAnimationController = ShakeAnimationController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (!widget.isSelected) {
          _shakeAnimationController.start(shakeCount: 1);
          if (widget.onPressed != null) {
            widget.onPressed!();
          }
        }
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ShakeAnimationWidget(
              shakeAnimationController: _shakeAnimationController,
              shakeAnimationType: widget.shakeAnimationType,
              shakeRange: 0.2,
              isForward: false,
              child:  Image.asset(
                widget.imageName,
                color: widget.isSelected ? primaryColor : Color(0xFF999999),
                width: 28,
              ),
            ),
            4.verticalSpace,
            TextWidget(
              widget.title,
              color: widget.isSelected ? widget.seletedTextColor : widget.textColor,
              fontSize: 14,
              fontWeight: widget.isSelected ? FontWeight.bold : FontWeight.normal,
            )
          ],
        ),
      ),
    );
  }
}


